﻿using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FuQinTube
{
    public class ExcelHandle
    {

        /// <summary>
        ///  
        /// </summary>
        /// <param name="fileName">excel文件名</param>
        /// <returns></returns>
        public async Task<List<Tube>> ReadExcel(string fileName)
        {
            List<Tube> tubes = new List<Tube>();
            await Task.Run(() =>
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) //打开excel文件
                {
                    IWorkbook sswb = WorkbookFactory.Create(fs);  //读取excel，可以兼融office2007之前的版本
                    ISheet sh = sswb.GetSheetAt(0);//读取第1和sheet
                    for (int i = 1; i <= sh.LastRowNum; i++) //遍历用过的所有行LastRowNum属性是这个工作表内所有已经用过的行数
                    {
                        IRow row = sh.GetRow(i); //获取其中的一行
                        tubes.Add(GetTube(row)); 
                    }
                }



            });
            return tubes;
        }

        private Tube GetTube(IRow row)
        {
            Tube tube = new Tube
            {
                INSTRUMENT_TAG = row.GetCell(0) == null ? "NA" : row.GetCell(0).ToString(),
                NUMBER = row.GetCell(1) == null ? "NA" : row.GetCell(1).ToString(),
                SYSTEM = row.GetCell(2) == null ? "NA" : row.GetCell(2).ToString(),
                FACTORY = row.GetCell(3) == null ? "NA" : row.GetCell(3).ToString(),
                ROOM_NUMBER = row.GetCell(4) == null ? "NA" : row.GetCell(4).ToString(),
                SHEET_CODE = row.GetCell(5) == null ? "NA" : row.GetCell(5).ToString(),
                P_ID_NUMBER = row.GetCell(6) == null ? "NA" : row.GetCell(6).ToString(),
                REMARKS = row.GetCell(7) == null ? "NA" : row.GetCell(7).ToString(),
                SYSTEM_CHINESE_NAME = row.GetCell(8) == null ? "NA" : row.GetCell(8).ToString(),
                DRAWING_NAME
 = row.GetCell(9) == null ? "NA" : row.GetCell(9).ToString(),
                DOC_NO = row.GetCell(10) == null ? "NA" : row.GetCell(10).ToString(),
                PIPELINE_DESIGN_PRESSURE = row.GetCell(11) == null ? "NA" : row.GetCell(11).ToString(),
                PIPELINE_DESIGN_TEMPERA = row.GetCell(12) == null ? "NA" : row.GetCell(12).ToString(),
                HYDRAULIC_TEST_PRESSURE = row.GetCell(13) == null ? "NA" : row.GetCell(13).ToString(),
                PRESSURE
 = row.GetCell(14) == null ? "NA" : row.GetCell(14).ToString(),
                TEMPERATURE = row.GetCell(15) == null ? "NA" : row.GetCell(15).ToString(),
          


            };
            return tube;



        }

    }
}
